﻿using DDD.Domain;
using Microsoft.EntityFrameworkCore;
using OrderModule.Domain.OrderItemDomainEvents;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace OrderModule.Domain.OrderAggregate
{
    [Comment("订单项")]
    [Table("WbOrderItem")]
    public class OrderItem : Entity<int>
    {
        /// <summary>
        /// 订单Id
        /// </summary>
        [Comment("订单Id")]
        public int OrderId { get; private set; } = 0;

        /// <summary>
        /// 商品Id
        /// </summary>
        [Comment("商品Id")]
        [Range(1, int.MaxValue)]
        public int GoodsId { get; private set; } = 0;

        /// <summary>
        /// 商品名
        /// </summary>
        [Comment("商品名")]
        [Required]
        [StringLength(20)]
        public string GoodsTitle { get; private set; } = string.Empty;

        /// <summary>
        /// 数量
        /// </summary>
        [Comment("数量")]
        [Required]
        public int Quantity { get; private set; } = 0;

        private OrderItem() { }

        public OrderItem(int goodsId, string goodsTitle, int quantity)
        {
            GoodsId = goodsId;
            GoodsTitle = goodsTitle ?? throw new ArgumentNullException(nameof(goodsTitle));
            Quantity = quantity;

            this.AddDomainEvent(new OrderItemCreatedDomainEvents(this));
        }
    }
}
